package rest

import (
	"encoding/json"
	"github.com/lishimeng/go-log"
	"github.com/pkg/errors"
	"io"
	"net/http"
)

func (rc *RestClient) afterExec(resp *http.Response) (code int, err error) {
	code = resp.StatusCode
	if DebugEnable {
		log.Debug("response code: %d", code)
	}
	if code != 200 { // http不成功
		return
	}

	if rc.jsonResp != nil {
		result, _ := io.ReadAll(resp.Body)

		var respStream []byte
		if rc.decrypt != nil {
			respStream, err = rc.decrypt(result)
			if err != nil {
				return
			}
		} else {
			respStream = result
		}

		err = json.Unmarshal(respStream, rc.jsonResp)
		if err != nil {
			err = errors.Wrap(err, "response json unmarshal err")
			if DebugEnable {
				log.Debug(err)
			}
			return
		}
	}
	return
}
